﻿using System;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;

namespace BeiraBaga
{
    public partial class Rewards : UserControl
    {
        #region Members

        private Controller _controller;

        #endregion

        #region Properties

        public Controller Controller { set { _controller = value; } }

        public GridView GridView { get { return gridView; } }

        #endregion

        public Rewards()
        {
            InitializeComponent();
        }

        #region Protected Overrides

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (_controller == null)
                return;
            tDU_EscaloesBindingSource.DataSource = _controller.Rewards;
        }

        #endregion

        #region Event Handlers

        private void gridView_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            TDU_Escaloes reward = (TDU_Escaloes)e.Row;
            _controller.Save(reward);
        }

        private void gridView_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.Column.Name != "monthGridColumn")
                return;
            TDU_Escaloes reward = (TDU_Escaloes)gridView.GetRow(e.RowHandle);
            if (e.IsGetData)
            {
                e.Value = reward.CDU_DataDesde;
                return;
            }
            if (e.Value == null && e.IsSetData)
                e.Value = reward.CDU_DataDesde;
            if (e.Value == null)
                return;
            DateTime date = (DateTime) e.Value;
            reward.CDU_DataDesde = new DateTime(date.Year, date.Month, 1);
            reward.CDU_DataAte = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
        }

        #endregion
    }
}
